<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据分析 - 美食社区</title>
    <!-- Bootstrap CSS -->
    <link href="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/css/bootstrap.min.css" rel="stylesheet">
    <!-- Font Awesome -->
    <link href="https://cdn.bootcdn.net/ajax/libs/font-awesome/6.4.2/css/all.min.css" rel="stylesheet">
    <!-- Chart.js -->
    <link href="https://cdn.bootcdn.net/ajax/libs/Chart.js/3.9.1/chart.min.css" rel="stylesheet">
    <link rel="stylesheet" th:href="@{/css/style.css}">
    <style>
        .dashboard-card {
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            margin-bottom: 20px;
            overflow: hidden;
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .dashboard-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .dashboard-card-header {
            padding: 15px 20px;
            background-color: #f8f9fa;
            border-bottom: 1px solid #e9ecef;
        }

        .dashboard-card-body {
            padding: 20px;
        }

        .stat-card {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            padding: 20px;
            margin-bottom: 20px;
            display: flex;
            align-items: center;
        }

        .stat-icon {
            width: 60px;
            height: 60px;
            border-radius: 50%;
            display: flex;
            align-items: center;
            justify-content: center;
            margin-right: 15px;
            font-size: 24px;
        }

        .stat-info h3 {
            margin: 0;
            font-size: 24px;
            font-weight: 700;
        }

        .stat-info p {
            margin: 0;
            color: #6c757d;
        }

        .bg-primary-light {
            background-color: rgba(13, 110, 253, 0.1);
            color: #0d6efd;
        }

        .bg-success-light {
            background-color: rgba(25, 135, 84, 0.1);
            color: #198754;
        }

        .bg-warning-light {
            background-color: rgba(255, 193, 7, 0.1);
            color: #ffc107;
        }

        .bg-info-light {
            background-color: rgba(13, 202, 240, 0.1);
            color: #0dcaf0;
        }

        .bg-orange-light {
            background-color: rgba(253, 126, 20, 0.1);
            color: #fd7e14;
        }

        .recommendation-card {
            border-radius: 10px;
            overflow: hidden;
            margin-bottom: 15px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            transition: transform 0.3s ease, box-shadow 0.3s ease;
        }

        .recommendation-card:hover {
            transform: translateY(-5px);
            box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
        }

        .recommendation-img {
            height: 150px;
            overflow: hidden;
        }

        .recommendation-img img {
            width: 100%;
            height: 100%;
            object-fit: cover;
            transition: transform 0.3s ease;
        }

        .recommendation-card:hover .recommendation-img img {
            transform: scale(1.1);
        }

        .recommendation-content {
            padding: 15px;
        }

        .recommendation-content h5 {
            margin-bottom: 5px;
            font-size: 16px;
        }

        .recommendation-content p {
            margin-bottom: 5px;
            font-size: 14px;
            color: #6c757d;
        }

        .rating-stars {
            color: #ffc107;
        }

        .nav-pills .nav-link.active {
            background-color: #fd7e14;
        }

        .nav-pills .nav-link {
            color: #495057;
        }

        .btn-primary {
            background-color: #fd7e14;
            border-color: #fd7e14;
        }

        .btn-primary:hover {
            background-color: #e76b00;
            border-color: #e76b00;
        }

        .badge-cuisine {
            background-color: #fff3e6;
            color: #fd7e14;
            border: 1px solid #ffd7b5;
            border-radius: 20px;
            padding: 5px 15px;
            margin: 5px;
            display: inline-block;
        }
    </style>
</head>

<body>
    <!-- 导航栏 -->
    <nav th:replace="~{common/header :: header}"></nav>

    <div class="container mt-5 pt-5">
        <div class="row">
            <div class="col-lg-8">
                <h2 class="section-title mb-4">数据分析</h2>

                <!-- 统计卡片 -->
                <div class="row mb-4">
                    <div class="col-md-6">
                        <div class="stat-card">
                            <div class="stat-icon bg-primary-light">
                                <i class="fas fa-utensils"></i>
                            </div>
                            <div class="stat-info">
                                <h3 th:text="${totalRestaurants}">0</h3>
                                <p>餐厅总数</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="stat-card">
                            <div class="stat-icon bg-success-light">
                                <i class="fas fa-book"></i>
                            </div>
                            <div class="stat-info">
                                <h3 th:text="${totalPosts}">0</h3>
                                <p>美食日记</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="stat-card">
                            <div class="stat-icon bg-warning-light">
                                <i class="fas fa-star"></i>
                            </div>
                            <div class="stat-info">
                                <h3 th:text="${totalReviews}">0</h3>
                                <p>餐厅评论</p>
                            </div>
                        </div>
                    </div>
                    <div class="col-md-6">
                        <div class="stat-card">
                            <div class="stat-icon bg-info-light">
                                <i class="fas fa-users"></i>
                            </div>
                            <div class="stat-info">
                                <h3 th:text="${totalUsers}">0</h3>
                                <p>社区用户</p>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 图表选项卡 -->
                <div class="dashboard-card">
                    <div class="dashboard-card-header">
                        <ul class="nav nav-pills" id="chartTabs" role="tablist">
                            <li class="nav-item" role="presentation">
                                <button class="nav-link active" id="cuisine-tab" data-bs-toggle="tab"
                                    data-bs-target="#cuisine" type="button" role="tab">菜系分布</button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="rating-tab" data-bs-toggle="tab" data-bs-target="#rating"
                                    type="button" role="tab">评分分布</button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="category-tab" data-bs-toggle="tab"
                                    data-bs-target="#category" type="button" role="tab">分类统计</button>
                            </li>
                            <li class="nav-item" role="presentation">
                                <button class="nav-link" id="trend-tab" data-bs-toggle="tab" data-bs-target="#trend"
                                    type="button" role="tab">月度趋势</button>
                            </li>
                        </ul>
                    </div>
                    <div class="dashboard-card-body">
                        <div class="tab-content" id="chartTabsContent">
                            <div class="tab-pane fade show active" id="cuisine" role="tabpanel">
                                <canvas id="cuisineChart" height="300"></canvas>
                            </div>
                            <div class="tab-pane fade" id="rating" role="tabpanel">
                                <canvas id="ratingChart" height="300"></canvas>
                            </div>
                            <div class="tab-pane fade" id="category" role="tabpanel">
                                <canvas id="categoryChart" height="300"></canvas>
                            </div>
                            <div class="tab-pane fade" id="trend" role="tabpanel">
                                <canvas id="trendChart" height="300"></canvas>
                            </div>
                        </div>
                    </div>
                </div>

                <!-- 热门餐厅排行榜 -->
                <div class="dashboard-card mt-4">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">热门餐厅排行榜</h5>
                    </div>
                    <div class="dashboard-card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>排名</th>
                                        <th>餐厅名称</th>
                                        <th>菜系</th>
                                        <th>评分</th>
                                        <th>评论数</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="restaurant, stat : ${hotRestaurants}">
                                        <td><span class="badge bg-primary" th:text="${stat.count}">1</span></td>
                                        <td>
                                            <a th:href="@{'/restaurant/' + ${restaurant.id}}"
                                                th:text="${restaurant.name}">餐厅名称</a>
                                        </td>
                                        <td><span class="badge-cuisine" th:text="${restaurant.cuisineType}">菜系</span>
                                        </td>
                                        <td>
                                            <span class="rating-stars">
                                                <span th:text="${restaurant.rating}">4.5</span>
                                                <i class="fas fa-star"></i>
                                            </span>
                                        </td>
                                        <td th:text="${restaurant.reviewCount}">123</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>

                <!-- 热门美食日记排行榜 -->
                <div class="dashboard-card mt-4">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">热门美食日记排行榜</h5>
                    </div>
                    <div class="dashboard-card-body">
                        <div class="table-responsive">
                            <table class="table table-hover">
                                <thead>
                                    <tr>
                                        <th>排名</th>
                                        <th>标题</th>
                                        <th>分类</th>
                                        <th>点赞数</th>
                                        <th>评论数</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr th:each="post, stat : ${hotPosts}">
                                        <td><span class="badge bg-primary" th:text="${stat.count}">1</span></td>
                                        <td>
                                            <a th:href="@{'/post/' + ${post.id}}" th:text="${post.title}">文章标题</a>
                                        </td>
                                        <td><span class="badge-cuisine" th:text="${post.categoryName}">分类</span></td>
                                        <td><i class="fas fa-heart text-danger me-1"></i> <span
                                                th:text="${post.likes}">123</span></td>
                                        <td><i class="fas fa-comment text-primary me-1"></i> <span
                                                th:text="${post.commentCount}">45</span></td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                </div>
            </div>

            <div class="col-lg-4">
                <!-- 用户偏好分析 -->
                <div class="dashboard-card" th:if="${session.user != null}">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">您的美食偏好</h5>
                    </div>
                    <div class="dashboard-card-body">
                        <h6>常浏览的菜系</h6>
                        <div class="mb-3">
                            <span th:each="cuisine : ${userPreferences?.preferredCuisineTypes}" class="badge-cuisine"
                                th:text="${cuisine.cuisine_type}">菜系</span>
                        </div>

                        <h6>常浏览的分类</h6>
                        <div class="mb-3">
                            <span th:each="category : ${userPreferences?.preferredCategories}" class="badge-cuisine"
                                th:text="${category.name}">分类</span>
                        </div>

                        <h6>平均评分</h6>
                        <div class="rating-stars mb-3">
                            <span
                                th:if="${userPreferences?.preferredCuisineTypes != null && !userPreferences.preferredCuisineTypes.isEmpty()}"
                                th:text="${userPreferences.preferredCuisineTypes[0].avg_rating != null ? #numbers.formatDecimal(userPreferences.preferredCuisineTypes[0].avg_rating, 1, 1) : '0.0'}">4.5</span>
                            <span
                                th:if="${userPreferences?.preferredCuisineTypes == null || userPreferences.preferredCuisineTypes.isEmpty()}">0.0</span>
                            <i class="fas fa-star"></i>
                        </div>

                        <a href="/user/profile" class="btn btn-outline-primary btn-sm">
                            <i class="fas fa-cog me-2"></i>更新偏好设置
                        </a>
                    </div>
                </div>

                <!-- 推荐餐厅 -->
                <div class="dashboard-card mt-4" th:if="${session.user != null}">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">为您推荐的餐厅</h5>
                    </div>
                    <div class="dashboard-card-body">
                        <div th:each="restaurant : ${recommendedRestaurants}" class="recommendation-card">
                            <div class="recommendation-img">
                                <img th:src="${restaurant.image}" alt="餐厅图片">
                            </div>
                            <div class="recommendation-content">
                                <h5>
                                    <a th:href="@{'/restaurant/' + ${restaurant.id}}"
                                        th:text="${restaurant.name}">餐厅名称</a>
                                </h5>
                                <p class="text-muted" th:text="${restaurant.address}">地址</p>
                                <div class="d-flex justify-content-between align-items-center">
                                    <div class="rating-stars">
                                        <span th:text="${restaurant.rating}">4.5</span>
                                        <i class="fas fa-star"></i>
                                    </div>
                                    <span class="badge-cuisine" th:text="${restaurant.cuisineType}">菜系</span>
                                </div>
                            </div>
                        </div>

                        <div class="text-center mt-3">
                            <a href="/restaurant" class="btn btn-primary btn-sm">
                                <i class="fas fa-utensils me-2"></i>查看更多餐厅
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 推荐美食日记 -->
                <div class="dashboard-card mt-4" th:if="${session.user != null}">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">为您推荐的美食日记</h5>
                    </div>
                    <div class="dashboard-card-body">
                        <div th:each="post : ${recommendedPosts}" class="recommendation-card">
                            <div class="recommendation-img">
                                <img th:src="${#strings.arraySplit(post.images, ',')[0]}" alt="美食图片">
                            </div>
                            <div class="recommendation-content">
                                <h5>
                                    <a th:href="@{'/post/' + ${post.id}}" th:text="${post.title}">文章标题</a>
                                </h5>
                                <p class="text-muted">
                                    <i class="fas fa-user me-1"></i>
                                    <span th:text="${post.nickname}">作者</span>
                                </p>
                                <div class="d-flex justify-content-between align-items-center">
                                    <div>
                                        <i class="fas fa-heart text-danger me-1"></i>
                                        <span th:text="${post.likes}">123</span>
                                    </div>
                                    <span class="badge-cuisine" th:text="${post.categoryName}">分类</span>
                                </div>
                            </div>
                        </div>

                        <div class="text-center mt-3">
                            <a href="/posts" class="btn btn-primary btn-sm">
                                <i class="fas fa-book me-2"></i>查看更多美食日记
                            </a>
                        </div>
                    </div>
                </div>

                <!-- 未登录提示 -->
                <div class="dashboard-card mt-4" th:if="${session.user == null}">
                    <div class="dashboard-card-header">
                        <h5 class="mb-0">个性化推荐</h5>
                    </div>
                    <div class="dashboard-card-body text-center">
                        <i class="fas fa-user-lock fa-3x text-muted mb-3"></i>
                        <h5>登录后查看个性化推荐</h5>
                        <p class="text-muted">登录后可以获取基于您浏览习惯的个性化餐厅和美食日记推荐</p>
                        <div class="d-grid gap-2">
                            <a href="/login" class="btn btn-primary">
                                <i class="fas fa-sign-in-alt me-2"></i>登录
                            </a>
                            <a href="/register" class="btn btn-outline-primary">
                                <i class="fas fa-user-plus me-2"></i>注册
                            </a>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Bootstrap Bundle with Popper -->
    <script src="https://cdn.bootcdn.net/ajax/libs/twitter-bootstrap/5.3.1/js/bootstrap.bundle.min.js"></script>
    <!-- jQuery -->
    <script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
    <!-- Chart.js -->
    <script src="https://cdn.bootcdn.net/ajax/libs/Chart.js/3.9.1/chart.min.js"></script>

    <script th:replace="~{common/header :: logout-script}"></script>


    <script th:inline="javascript">
        $(document).ready(function () {
            // 获取数据
            const cuisineTypeStats = /*[[${analysis.cuisineTypeStats}]]*/[];
            const ratingDistribution = /*[[${analysis.ratingDistribution}]]*/[];
            const categoryStats = /*[[${analysis.categoryStats}]]*/[];
            const monthlyTrends = /*[[${analysis.monthlyTrends}]]*/[];

            // 菜系分布图表
            if (cuisineTypeStats && cuisineTypeStats.length > 0) {
                const cuisineLabels = cuisineTypeStats.map(item => item.cuisine_type);
                const cuisineData = cuisineTypeStats.map(item => item.restaurant_count);
                const cuisineColors = generateColors(cuisineLabels.length);

                new Chart(document.getElementById('cuisineChart'), {
                    type: 'pie',
                    data: {
                        labels: cuisineLabels,
                        datasets: [{
                            data: cuisineData,
                            backgroundColor: cuisineColors,
                            borderWidth: 1
                        }]
                    },
                    options: {
                        responsive: true,
                        plugins: {
                            legend: {
                                position: 'right',
                            },
                            title: {
                                display: true,
                                text: '餐厅菜系分布'
                            }
                        }
                    }
                });
            }

            // 评分分布图表
            if (ratingDistribution && ratingDistribution.length > 0) {
                const ratingLabels = ratingDistribution.map(item => item.rating + '分');
                const ratingData = ratingDistribution.map(item => item.count);

                new Chart(document.getElementById('ratingChart'), {
                    type: 'bar',
                    data: {
                        labels: ratingLabels,
                        datasets: [{
                            label: '评论数量',
                            data: ratingData,
                            backgroundColor: 'rgba(253, 126, 20, 0.7)',
                            borderColor: 'rgba(253, 126, 20, 1)',
                            borderWidth: 1
                        }]
                    },
                    options: {
                        responsive: true,
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        },
                        plugins: {
                            title: {
                                display: true,
                                text: '餐厅评分分布'
                            }
                        }
                    }
                });
            }

            // 分类统计图表
            if (categoryStats && categoryStats.length > 0) {
                const categoryLabels = categoryStats.map(item => item.name);
                const categoryData = categoryStats.map(item => item.post_count);
                const categoryColors = generateColors(categoryLabels.length);

                new Chart(document.getElementById('categoryChart'), {
                    type: 'doughnut',
                    data: {
                        labels: categoryLabels,
                        datasets: [{
                            data: categoryData,
                            backgroundColor: categoryColors,
                            borderWidth: 1
                        }]
                    },
                    options: {
                        responsive: true,
                        plugins: {
                            legend: {
                                position: 'right',
                            },
                            title: {
                                display: true,
                                text: '美食日记分类统计'
                            }
                        }
                    }
                });
            }

            // 月度趋势图表
            if (monthlyTrends && monthlyTrends.length > 0) {
                const trendLabels = monthlyTrends.map(item => item.month);
                const postData = monthlyTrends.map(item => item.post_count);
                const commentData = monthlyTrends.map(item => item.comment_count);
                const reviewData = monthlyTrends.map(item => item.review_count);

                new Chart(document.getElementById('trendChart'), {
                    type: 'line',
                    data: {
                        labels: trendLabels,
                        datasets: [
                            {
                                label: '美食日记',
                                data: postData,
                                borderColor: 'rgba(13, 110, 253, 1)',
                                backgroundColor: 'rgba(13, 110, 253, 0.1)',
                                tension: 0.3,
                                fill: true
                            },
                            {
                                label: '评论',
                                data: commentData,
                                borderColor: 'rgba(25, 135, 84, 1)',
                                backgroundColor: 'rgba(25, 135, 84, 0.1)',
                                tension: 0.3,
                                fill: true
                            },
                            {
                                label: '餐厅评价',
                                data: reviewData,
                                borderColor: 'rgba(253, 126, 20, 1)',
                                backgroundColor: 'rgba(253, 126, 20, 0.1)',
                                tension: 0.3,
                                fill: true
                            }
                        ]
                    },
                    options: {
                        responsive: true,
                        scales: {
                            y: {
                                beginAtZero: true
                            }
                        },
                        plugins: {
                            title: {
                                display: true,
                                text: '月度活跃度趋势'
                            }
                        }
                    }
                });
            }

            // 生成随机颜色
            function generateColors(count) {
                const colors = [];
                const baseColors = [
                    'rgba(253, 126, 20, 0.7)',  // 橙色
                    'rgba(13, 110, 253, 0.7)',  // 蓝色
                    'rgba(25, 135, 84, 0.7)',   // 绿色
                    'rgba(220, 53, 69, 0.7)',   // 红色
                    'rgba(255, 193, 7, 0.7)',   // 黄色
                    'rgba(13, 202, 240, 0.7)',  // 青色
                    'rgba(111, 66, 193, 0.7)',  // 紫色
                    'rgba(102, 16, 242, 0.7)',  // 靛蓝色
                    'rgba(214, 51, 132, 0.7)',  // 粉色
                    'rgba(108, 117, 125, 0.7)'  // 灰色
                ];

                for (let i = 0; i < count; i++) {
                    colors.push(baseColors[i % baseColors.length]);
                }

                return colors;
            }
        });
    </script>
</body>

</html>